package com.jh.controller;

import com.jh.pojo.Dept;
import com.jh.pojo.Result;
import com.jh.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.ResolverUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询部门列表
     * @return
     */
    @GetMapping
    public Result list(){
        log.info("查询部门列表");
        List<Dept> list= deptService.list();
        return Result.success(list);
    }

    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @DeleteMapping
    public Result delete(Integer id){
        log.info("删除"+id+"部门");
        deptService.delete(id);
        return Result.success();
    }

    /**
     * 添加部门
     * 对于json格式传入的数据，方法形参要加注解@RequestBody
     * @param dept
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加部门："+dept.getName());
        deptService.add(dept);
        return Result.success();
    }

    /**
     * 根据id来查询部门
     * 路径参数用@PathVariable
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result searchById(@PathVariable Integer id){
        log.info("根据id查询部门："+id);
        Dept list=deptService.searchById(id);
        return Result.success(list);
    }

    /**
     * 修改部门
     * @param dept
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Dept dept){
        log.info("修改的部门："+dept);
        deptService.update(dept);
        return Result.success();
    }

}
